


const  url = require('url');
const  fs =require('fs');
const  sharp =require('sharp');
const  path = require('path');
const { exec ,execSync} = require('child_process');

const dirname = process.cwd();
const zip = (file,quality=80) => {
    console.log('=======================================>')
    // 将文件保存后并压缩
    return new Promise((resolve, reject) => {
        const outputPath = path.join(dirname, 'input.png');
        
        sharp(file.filepath)
            .toFile(outputPath, (err, info) => {
                if (err) {
                    return reject('转换失败')
                    
                }
                const inputPath = path.join(dirname, 'input.png');
                const outputPath = path.join(dirname, 'output.png');
                const command = `pngquant ${inputPath} --speed=1 --quality=${quality} --force -o ${outputPath}`;
                exec(command, (error, stdout, stderr) => {
                    if (error) {
                        return reject('压缩失败')
                    }
                    fs.readFile(outputPath, (err, data) => {
                        if (err) {
                            return reject('读取失败')
                        }
                        console.log('压缩成功 sssssssssssssssssssssss');
                        // 删除临时文件
                        resolve(data);
                        fs.unlinkSync(inputPath);
                        fs.unlinkSync(outputPath);
                    });
                });
            });
    })
    // 获取保存的图片

}

/***
 * /**
 * 1.下载 https://ffmpeg.github.net.cn/download.html
        选择 Windows 从 gyan.dev 构建
    
    2.添加环境变量，可能需要重启电脑   

const {execSync} = require('child_process');
// 转gif
// const command = 'ffmpeg -i xingtu.mp4 test.gif';
// 转mp3
// const command = 'ffmpeg -i xingtu.mp4 -vn -ar 44100 -ac 2 -ab 192k -f mp3 test.mp3';
// 转avi
// 裁剪视频
// const command = 'ffmpeg -i xingtu.mp4 -vcodec libx264 -acodec aac -strict experimental -b:v 1000k -bufsize 2000k -maxrate 1000k -vf scale=640:360 test.avi';
// const command = 'ffmpeg -i xingtu.mp4 -ss 10 -t 00:00:20 clip1.mp4';
// 合并视频
// const command = 'ffmpeg -i "concat:clip.mp4|clip1.mp4" -c copy output.mp4';
// 添加水印
// const command = 'ffmpeg -i xingtu.mp4 -i logo.png -filter_complex "overlay=10:10" water.mp4';
// 添加文字水印
// const command = `ffmpeg -i xingtu.mp4 -vf drawtext="text='Hello, World!':fontfile=Arial.ttf:fontsize=36:x=10:y=10" watertext.mp4`;
// 去除水印 w h
const command = `ffmpeg -i watertext.mp4 -vf delogo=w=300:h=106:x=10:y=10  -af "areverse" clearwatertext.mp4`;
const stdio = {stdio: 'inherit'} //终端提示
execSync(command,stdio)
 * 
*/

// 编辑视频
const stdio = {stdio: 'inherit'} //终端提示
const videoTools = (config)=>{
    return new Promise((resolve, reject) => {
        
        const {name,type} = config
        let inputPath = path.join(process.cwd(), '/router/tools/files',name)
        let command = ''
        let outputPath,outputName
        if(type == 'gif'){
            command = `ffmpeg -i ${inputPath} ${inputPath}.gif`
            outputPath = path.join(process.cwd(), '/router/tools/files',name+'.gif')
            outputName = name+'.gif'
        }
        if(type == 'mp3'){
            command = `ffmpeg -i ${inputPath} -vn -ar 44100 -ac 2 -ab 192k -f mp3 ${inputPath}.mp3`
            outputPath = path.join(process.cwd(), '/router/tools/files',name+'.mp3')
            outputName = name+'.mp3'
        }
        if(type == 'clip'){
            command =`ffmpeg -i ${inputPath} -ss ${config.begin} -t ${config.end} ${inputPath}.mp4`;
            outputPath = path.join(process.cwd(), '/router/tools/files',name+'.mp4')
            outputName = name+'.mp4'
        }
        if(type == 'text-watermark'){
            // 汉字需要引入字体
            command = `ffmpeg -i  ${inputPath} -vf drawtext="text=${config.text}:fontsize=36:x=10:y=10" ${inputPath}.mp4`;
            outputPath = path.join(process.cwd(), '/router/tools/files',name+'.mp4')
            outputName = name+'.mp4'
        }
        if(type == 'remove-watermark'){
            command =`ffmpeg -i ${inputPath} -vf delogo=w=300:h=106:x=10:y=10  -af "areverse" ${inputPath}.mp4`;
            outputPath = path.join(process.cwd(), '/router/tools/files',name+'.mp4')
            outputName = name+'.mp4'
        }
        execSync(command,stdio)
        fs.unlinkSync(inputPath)
        resolve(outputName)
    })
}



module.exports = {
    zip,
    videoTools
}